#include "BondDetailChartTitle.h"
#include "BondDetailData.h"
#include <QStylePainter>
#include <core/StyleMgr.h>
#include <widgets/SComboBox.h>
#include <widgets/STabCtrl.h>
#include <widgets/SCheckEx.h>
#include <widgets/SImageButton.h>
#include <widgets/DualCalendars.h>
#include <widgets/SCheckComboBox.h>
#include <bondlib/BondDict.h>
#include <QSpacerItem>
#include <QHBoxLayout>
#include <QCheckBox>

BondDetailDealChartTitle::BondDetailDealChartTitle(QWidget* parent)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setupUi();
}

BondDetailDealChartTitle::~BondDetailDealChartTitle()
{

}

void BondDetailDealChartTitle::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);

	painter.fillRect(rect(), StyleMgr::instance().color("BasicPlate"));

	QRect titleR = rect();
	titleR.setHeight(24);

	QString title = tr("成交");
	QString broker = tr("经纪商");

	if (m_bondType == em_CFETS)
		broker = tr("CFETS");

	QFontMetrics fm(font());
	int widthTxt = fm.horizontalAdvance(title);

	QRect r = fm.boundingRect(broker);
	r.adjust(-2, -2, 2, 2);

	int titleWidth = widthTxt + 4 + r.width();
	int titleX = (titleR.center().x() - titleWidth / 2);

	QRect titleRect;
	titleRect.setRect(titleX, titleR.y() + 4, widthTxt, titleR.height());
	painter.setPen(StyleMgr::instance().color("TextNormal"));
	painter.drawText(titleRect, title, QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

	QRect brokerRect;
	int	brokerX = titleX + 6 + widthTxt;
	widthTxt = fm.horizontalAdvance(broker);
	brokerRect.setRect(brokerX, titleR.y() + 4, widthTxt, titleR.height());
	painter.setPen(StyleMgr::instance().color("BrokerAll"));
	if (m_bondType == em_CFETS)
		painter.setPen(StyleMgr::instance().color("BrokerCFETS"));
	painter.drawText(brokerRect, broker, QTextOption(Qt::AlignRight | Qt::AlignVCenter));
	brokerRect.adjust(-2, 4, 1, -5);
	painter.drawRoundedRect(brokerRect, 1, 1);
}

void BondDetailDealChartTitle::setupUi()
{
	m_layout = new QHBoxLayout(this);
	m_layout->setContentsMargins(0, 25, 0, 0);
	m_layout->setSpacing(4);
	
	m_layout->addStretch(2);
	m_BrokerDealTab = new STabCtrl(this);
	m_BrokerDealTab->addTab(tr("日线"));
	m_BrokerDealTab->addTab(tr("实时"));
	m_BrokerDealTab->selectTab(1);
	m_BrokerDealTab->setFixedSize(80, 24);
	m_layout->addWidget(m_BrokerDealTab);

	m_BrokerDayClosedTab = new STabCtrl(this);
	m_BrokerDayClosedTab->addTab(tr("日均"));
	m_BrokerDayClosedTab->addTab(tr("收盘"));
	m_BrokerDayClosedTab->setFixedSize(80, 24);
	m_layout->insertWidget(4, m_BrokerDayClosedTab);
	m_BrokerDayClosedTab->hide();

	m_BrokerDealDateTab = new STabCtrl(this);
	m_BrokerDealDateTab->addTab(tr("1M"));
	m_BrokerDealDateTab->addTab(tr("3M"));
	m_BrokerDealDateTab->addTab(tr("6M"));
	m_BrokerDealDateTab->addTab(tr("YTD"));
	m_BrokerDealDateTab->addTab(tr("1Y"));
	m_BrokerDealDateTab->addTab(tr("ALL"));
	m_BrokerDealDateTab->selectTab(0);
	m_BrokerDealDateTab->setFixedSize(240, 24);
	m_layout->addWidget(m_BrokerDealDateTab);

	m_BrokerDateBtn = new SImageButton(this, "widgets_calendar");
	m_layout->addWidget(m_BrokerDateBtn);
	m_BrokerDateBtn->hide();
	m_BrokerDualCalendars = new DualCalendars(this, false);
	m_BrokerDualCalendars->hide();
	m_BrokerDealDateTab->hide();

	m_CFETSdealTab = new STabCtrl(this);
	m_CFETSdealTab->addTab(tr("日线"));
	m_CFETSdealTab->addTab(tr("实时"));
	m_CFETSdealTab->selectTab(1);
	m_CFETSdealTab->setFixedSize(80, 24);
	m_layout->addWidget(m_CFETSdealTab);
	m_CFETSdealTab->hide();

	m_CFETSdayClosedTab = new STabCtrl(this);
	m_CFETSdayClosedTab->addTab(tr("日均"));
	m_CFETSdayClosedTab->addTab(tr("收盘"));
	m_CFETSdayClosedTab->setFixedSize(80, 24);
	m_layout->insertWidget(5, m_CFETSdayClosedTab);
	m_CFETSdayClosedTab->hide();

	m_CFETSDealDateTab = new STabCtrl(this);
	m_CFETSDealDateTab->addTab(tr("1M"));
	m_CFETSDealDateTab->addTab(tr("3M"));
	m_CFETSDealDateTab->addTab(tr("6M"));
	m_CFETSDealDateTab->addTab(tr("YTD"));
	m_CFETSDealDateTab->addTab(tr("1Y"));
	m_CFETSDealDateTab->addTab(tr("ALL"));
	m_CFETSDealDateTab->selectTab(0);
	m_CFETSDealDateTab->setFixedSize(240, 24);
	m_layout->addWidget(m_CFETSDealDateTab);
	m_CFETSDealDateTab->hide();
	m_CFETSDateBtn = new SImageButton(this, "widgets_calendar");
	m_layout->addWidget(m_CFETSDateBtn);
	m_CFETSDateBtn->hide();
	m_CFETSDualCalendars = new DualCalendars(this, false);
	m_CFETSDualCalendars->hide();
	m_layout->addStretch(2);

	refreshUi();

	connect(m_BrokerDealTab, &STabCtrl::tabSelected, this, &BondDetailDealChartTitle::onBrokerTabDealSelected);
	connect(m_BrokerDayClosedTab, &STabCtrl::tabSelected, this, &BondDetailDealChartTitle::onBrokerTabDaySelected);

	connect(m_CFETSdealTab, &STabCtrl::tabSelected, this, &BondDetailDealChartTitle::onCFETSTabDealSelected);
	connect(m_CFETSdayClosedTab, &STabCtrl::tabSelected, this, &BondDetailDealChartTitle::onCFETSTabDaySelected);

	connect(m_BrokerDealDateTab,&STabCtrl::tabSelected,this, &BondDetailDealChartTitle::onBrokerDateTabSelected);
	connect(m_CFETSDealDateTab,&STabCtrl::tabSelected,this, &BondDetailDealChartTitle::onCFETSDateTabSelected);
	connect(m_BrokerDateBtn, &SImageButton::clicked, this, &BondDetailDealChartTitle::onBrokerDateBtnSelected);
	connect(m_CFETSDateBtn, &SImageButton::clicked, this, &BondDetailDealChartTitle::onCFETSDateBtnSelected);
}

void BondDetailDealChartTitle::setBondInfo(QSharedPointer<DetailData> data)
{
	QB_CHECK_RETURN_VOID1(data);

	if (data)
	{
		m_bondType = data->getBondType();
	}

	m_data = data;

    connect(m_data.get(), &DetailData::bondTypeChanged, this, &BondDetailDealChartTitle::onBondTypeChanged);

	refreshUi();
}

void BondDetailDealChartTitle::changeType(int start, int end)
{
	QString startDay = QString::number(start);
	QString endDay = QString::number(end);

	startD = QDate::fromString(startDay, "yyyyMMdd");
	endD = QDate::fromString(endDay, "yyyyMMdd");
}

void BondDetailDealChartTitle::refreshUi()
{
	QB_CHECK_RETURN_VOID1(m_CFETSdayClosedTab && m_CFETSdealTab && m_BrokerDealTab && m_BrokerDayClosedTab && m_CFETSDealDateTab && m_BrokerDealDateTab && m_BrokerDateBtn && m_CFETSDateBtn);

	if (m_bondType == em_Broker)
	{
		m_CFETSDealDateTab->hide();
		m_CFETSdayClosedTab->hide();
		m_CFETSdealTab->hide();
		m_CFETSDateBtn->hide();

		m_BrokerDealTab->show();

		if (m_BrokerDay)
		{
			m_BrokerDayClosedTab->show();
			m_BrokerDealDateTab->show();
			m_BrokerDateBtn->show();
		}
	}
	else
	{
		m_BrokerDealDateTab->hide();
		m_BrokerDayClosedTab->hide();
		m_BrokerDealTab->hide();
		m_BrokerDateBtn->hide();

		m_CFETSdealTab->show();

		if (m_CFETSDay) 
		{
			m_CFETSdayClosedTab->show();
			m_CFETSDealDateTab->show();
			m_CFETSDateBtn->show();
		}
	}
}

void BondDetailDealChartTitle::notify()
{
	QB_CHECK_RETURN_VOID1(m_CFETSdayClosedTab && m_BrokerDayClosedTab && m_BrokerDealDateTab && m_CFETSDealDateTab);

	if (m_bondType == em_Broker)
	{
		if (m_BrokerDay)
		{
			m_BrokerDayClosedTab->getSelTab() == 0 ? emit dayAverTabSelected() : emit dayClosedTabSelected();
			emit dateTabSelected(m_BrokerDealDateTab->getSelTab());
		}
		else
		{
			emit realTimeTabSelected();
		}
	}
	else
	{
		if (m_CFETSDay)
		{ 
			m_CFETSdayClosedTab->getSelTab() == 0 ? emit dayAverTabSelected() : emit dayClosedTabSelected();
			emit dateTabSelected(m_CFETSDealDateTab->getSelTab());
		}
		else
		{
			emit realTimeTabSelected();
		}
	}
}

void BondDetailDealChartTitle::clearCalendarlastSelected()
{
	QDate date;
	if (m_bondType == em_Broker)
	{
		m_BrokerDualCalendars->getLeftSCalendar()->setStart(date);
		m_BrokerDualCalendars->getRightSCalendar()->setStart(date);
		m_BrokerDualCalendars->getLeftSCalendar()->setEnd(date);
		m_BrokerDualCalendars->getRightSCalendar()->setEnd(date);
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
		m_BrokerDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
	}
	else
	{
		m_CFETSDualCalendars->getLeftSCalendar()->setStart(date);
		m_CFETSDualCalendars->getRightSCalendar()->setStart(date);
		m_CFETSDualCalendars->getLeftSCalendar()->setEnd(date);
		m_CFETSDualCalendars->getRightSCalendar()->setEnd(date);
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
		m_CFETSDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
	}
}

void BondDetailDealChartTitle::onBondTypeChanged()
{
	QB_CHECK_RETURN_VOID1(m_data);
	m_bondType = m_data->getBondType();
	refreshUi();
	notify();
	update();
}

void BondDetailDealChartTitle::onBrokerTabDealSelected(int index)
{	
	QB_CHECK_RETURN_VOID1(m_BrokerDayClosedTab && m_BrokerDealDateTab);
	if (index == 0)
	{
		m_BrokerDayClosedTab->show();
		m_BrokerDayClosedTab->selectTab(m_BrokerIndex); //仅选中
		onBrokerTabDaySelected(m_BrokerIndex);  // 选中后的操作
		m_BrokerDealDateTab->show();
		m_BrokerDealDateTab->selectTab(0);
		m_BrokerDateBtn->show();
		emit dateTabSelected(0);
	}
	else
	{
		m_BrokerDay = false;
		m_BrokerDayClosedTab->hide();
		m_BrokerDealDateTab->hide();
		m_BrokerDateBtn->hide();
		emit  realTimeTabSelected();
	}
}

void BondDetailDealChartTitle::onBrokerTabDaySelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_BrokerDealDateTab);
	m_BrokerIndex = index;
	index == 0 ? emit dayAverTabSelected() : emit dayClosedTabSelected();
	emit dateTabSelected(m_BrokerDealDateTab->getSelTab());
	m_BrokerDay = true;
}

void BondDetailDealChartTitle::onCFETSTabDealSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_CFETSdayClosedTab && m_CFETSDealDateTab);

	if (index == 0)
	{
		m_CFETSdayClosedTab->show();
		m_CFETSdayClosedTab->selectTab(m_CFETSindex);
		onCFETSTabDaySelected(m_CFETSindex);
		m_CFETSDealDateTab->show();
		m_CFETSDealDateTab->selectTab(0);
		m_CFETSDateBtn->show();
		emit dateTabSelected(0);
	}
	else
	{
		m_CFETSDay = false;
		m_CFETSdayClosedTab->hide();
		m_CFETSDealDateTab->hide();
		m_CFETSDateBtn->hide();
		emit  realTimeTabSelected();
	}
}

void BondDetailDealChartTitle::onCFETSTabDaySelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_CFETSDealDateTab);
	m_CFETSindex = index;
	index == 0 ? emit dayAverTabSelected() : emit dayClosedTabSelected();
	emit dateTabSelected(m_CFETSDealDateTab->getSelTab());
	m_CFETSDay = true;
}

void BondDetailDealChartTitle::onBrokerDateTabSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_BrokerDayClosedTab);
	m_BrokerDayClosedTab->getSelTab() == 0 ? emit dayAverTabSelected() : emit dayClosedTabSelected();
	emit dateTabSelected(index);
}

void BondDetailDealChartTitle::onCFETSDateTabSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_CFETSdayClosedTab);
	m_CFETSdayClosedTab->getSelTab() == 0 ? emit dayAverTabSelected() : emit dayClosedTabSelected();
	emit dateTabSelected(index);
}

void BondDetailDealChartTitle::onBrokerDateBtnSelected()
{
	QB_CHECK_RETURN_VOID1(m_BrokerDualCalendars && m_BrokerDateBtn);
	int w = m_BrokerDualCalendars->width();
	int h = m_BrokerDualCalendars->height();
	QPoint p = m_BrokerDateBtn->pos();
	int x = mapToGlobal(p).x();
	int y = mapToGlobal(p).y();
	m_BrokerDualCalendars->setGeometry(x + 15, y + 15, w, h);
	m_BrokerDualCalendars->show();
	clearCalendarlastSelected();
	if (!dateIsValid) //无效日期
	{
		QDate currentDay(QDate::currentDate());
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedDate(currentDay.addMonths(-1));
		m_BrokerDualCalendars->getRightSCalendar()->showToday();
	}
	else
	{
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedDate(startD);
		if (startD.month() == endD.month())
		{
			m_BrokerDualCalendars->getRightSCalendar()->setSelectedDate(endD.addMonths(1));
		}
		else
		{
			m_BrokerDualCalendars->getRightSCalendar()->setSelectedDate(endD);
		}
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
		m_BrokerDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
	}
}

void BondDetailDealChartTitle::onCFETSDateBtnSelected()
{
	QB_CHECK_RETURN_VOID1(m_CFETSDualCalendars && m_CFETSDateBtn);
	int w = m_CFETSDualCalendars->width();
	int h = m_CFETSDualCalendars->height();
	QPoint p = m_CFETSDateBtn->pos();
	int x = mapToGlobal(p).x();
	int y = mapToGlobal(p).y();
	m_CFETSDualCalendars->setGeometry(x + 15, y + 15, w, h);
	m_CFETSDualCalendars->show();
	clearCalendarlastSelected();
	if (!dateIsValid) //无效日期
	{
		QDate currentDay(QDate::currentDate());
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedDate(currentDay.addMonths(-1));
		m_CFETSDualCalendars->getRightSCalendar()->showToday();
	}
	else
	{
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedDate(startD);
		if (startD.month() == endD.month())
		{
			m_CFETSDualCalendars->getRightSCalendar()->setSelectedDate(endD.addMonths(1));
		}
		else
		{
			m_CFETSDualCalendars->getRightSCalendar()->setSelectedDate(endD);
		}
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
		m_CFETSDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
	}
}

void BondDetailDealChartTitle::onBondDateBtnChanged()
{
	QB_CHECK_RETURN_VOID1(m_BrokerDealDateTab && m_CFETSDealDateTab);
	if (m_bondType == em_Broker)
	{
		m_BrokerDealDateTab->selectTab(-1);
	}
	else
	{
		m_CFETSDealDateTab->selectTab(-1);
	}
}

void BondDetailDealChartTitle::onDualCalendarsGetDate(int start, int end)
{
	if (m_bondType == em_Broker)
	{
		if (start == 0 && end == 0)
		{
			dateIsValid = false;
		}
		else
		{
			changeType(start, end);
			dateIsValid = true;
		}
	}
	else
	{
		if (start == 0 && end == 0)
		{
			dateIsValid = false;
		}
		else
		{
			changeType(start, end);
			dateIsValid = true;
		}
	}
}

BondDetailQuoteChartTitle::BondDetailQuoteChartTitle(QWidget* parent)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setupUi();
}

BondDetailQuoteChartTitle::~BondDetailQuoteChartTitle()
{

}

void BondDetailQuoteChartTitle::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);

	painter.fillRect(rect(), StyleMgr::instance().color("BasicPlate"));

	QRect titleR = rect();
	titleR.setHeight(24);

	QString title = tr("报价");
	QString broker = tr("经纪商");

	if (m_bondType == em_CFETS)
		broker = tr("CFETS");

	QFontMetrics fm(font());
	int widthTxt = fm.horizontalAdvance(title);

	QRect r = fm.boundingRect(broker);
	r.adjust(-2, -2, 2, 2);

	int titleWidth = widthTxt + 4 + r.width();
	int titleX = (titleR.center().x() - titleWidth / 2);

	QRect titleRect;
	titleRect.setRect(titleX, titleR.y() + 4, widthTxt, titleR.height());

	painter.setPen(StyleMgr::instance().color("TextNormal"));
	painter.drawText(titleRect, title, QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

	QRect brokerRect;
	int	brokerX = titleX + 6 + widthTxt;
	widthTxt = fm.horizontalAdvance(broker);
	brokerRect.setRect(brokerX, titleR.y() + 4, widthTxt, titleR.height());
	painter.setPen(StyleMgr::instance().color("BrokerAll"));
	if (m_bondType == em_CFETS)
		painter.setPen(StyleMgr::instance().color("BrokerCFETS"));
	painter.drawText(brokerRect, broker, QTextOption(Qt::AlignRight | Qt::AlignVCenter));
	brokerRect.adjust(-2, 4, 1, -5);
	painter.drawRoundedRect(brokerRect, 1, 1);

}

void BondDetailQuoteChartTitle::setupUi()
{
	m_rateLayout = new QHBoxLayout(this);
	m_rateLayout->setSpacing(4);
	m_rateLayout->setContentsMargins(0, 25, 0, 0);

	m_BrokerFilAbnValCheck = new SCheckEx(tr("剔除异常值"), this);
	m_BrokerFilAbnValCheck->setSelectType(SCheckEx::SelectType::Switch);
	m_BrokerFilAbnValCheck->setFixedSize(QSize(96, 24));
	m_BrokerFilAbnValCheck->setSelectState(Qt::Checked);
	m_rateLayout->addStretch();
	m_rateLayout->addStretch();
	m_rateLayout->addWidget(m_BrokerFilAbnValCheck);

	m_BrokerCombox = new SComboBox(this);
	QStringList strList;
	strList << tr("收益率") << tr("净价");
	m_BrokerCombox->addItems(strList);
	m_BrokerCombox->setFixedSize(QSize(96, 24));
	m_rateLayout->insertWidget(4, m_BrokerCombox);


	m_CFETSFilAbnValCheck = new SCheckEx(tr("剔除异常值"), this);
	m_CFETSFilAbnValCheck->setSelectType(SCheckEx::SelectType::Switch);
	m_CFETSFilAbnValCheck->setFixedSize(QSize(96, 24));
	m_CFETSFilAbnValCheck->setSelectState(Qt::Checked);
	m_rateLayout->addWidget(m_CFETSFilAbnValCheck);
	m_CFETSFilAbnValCheck->hide();

	m_CFETSCombox = new SComboBox(this);
	QStringList CFETSList;
	CFETSList << tr("收益率") << tr("净价");
	m_CFETSCombox->addItems(CFETSList);
	m_CFETSCombox->setFixedSize(QSize(96, 24));
	m_rateLayout->addWidget(m_CFETSCombox);
	m_CFETSCombox->hide();
	m_rateLayout->addStretch();
	m_rateLayout->addStretch();

	connect(m_BrokerFilAbnValCheck, &SCheckEx::stateChanged, this, &BondDetailQuoteChartTitle::onBrokerStateChanged);
	connect(m_CFETSFilAbnValCheck, &SCheckEx::stateChanged, this, &BondDetailQuoteChartTitle::onCFETSStateChanged);
	connect(m_BrokerCombox, QOverload<int>::of(&SComboBox::currentIndexChanged), this, &BondDetailQuoteChartTitle::onBrokerItemSelected);
	connect(m_CFETSCombox, QOverload<int>::of(&SComboBox::currentIndexChanged), this, &BondDetailQuoteChartTitle::onCFETSItemSelected);
}

void BondDetailQuoteChartTitle::setBondInfo(QSharedPointer<DetailData> data)
{
	QB_CHECK_RETURN_VOID1(data);

	m_bondType = data->getBondType();
	m_data = data;

    connect(m_data.get(), &DetailData::bondTypeChanged, this, &BondDetailQuoteChartTitle::onBondTypeChanged);

	refreshUi();
}

void BondDetailQuoteChartTitle::onBondTypeChanged()
{
	QB_CHECK_RETURN_VOID1(m_data);

	m_bondType = m_data->getBondType();

	refreshUi();
	notify();
	update();
}

void BondDetailQuoteChartTitle::onBrokerStateChanged()
{
	QB_CHECK_RETURN_VOID1(m_BrokerFilAbnValCheck);

	m_BrokerFilAbnValCheck->isSelected() == true ? emit filterAbValueSelected(1) : emit filterAbValueSelected(0);
}

void BondDetailQuoteChartTitle::onCFETSStateChanged()
{
	QB_CHECK_RETURN_VOID1(m_CFETSFilAbnValCheck);

	m_CFETSFilAbnValCheck->isSelected() == true ? emit filterAbValueSelected(1) : emit filterAbValueSelected(0);
}

void BondDetailQuoteChartTitle::refreshUi()
{
	QB_CHECK_RETURN_VOID1(m_data && m_BrokerFilAbnValCheck && m_CFETSFilAbnValCheck && m_CFETSCombox && m_BrokerCombox);
	
	if (m_bondType == em_Broker)
	{
		m_data->bond().is_interest_bond() == true ? m_BrokerFilAbnValCheck->show() : m_BrokerFilAbnValCheck->hide();
		m_CFETSFilAbnValCheck->hide();
		m_CFETSCombox->hide();
		m_BrokerCombox->show();
	}
	else
	{
		m_data->bond().is_interest_bond() == true ? m_CFETSFilAbnValCheck->show() :	m_CFETSFilAbnValCheck->hide();
		m_BrokerFilAbnValCheck->hide();
		m_BrokerCombox->hide();
		m_CFETSCombox->show();
	}
}

void BondDetailQuoteChartTitle::notify()
{
	QB_CHECK_RETURN_VOID1(m_data && m_BrokerFilAbnValCheck && m_CFETSFilAbnValCheck);

	if (m_bondType == em_Broker)
	{
		if (m_BrokerIndex == 0)
		{
			if (m_data->bond().is_interest_bond())
			{
				m_BrokerFilAbnValCheck->show();

				m_BrokerFilAbnValCheck->isSelected() == true ? emit filterAbValueSelected(1): emit filterAbValueSelected(0);

			}
			emit yieldSelected();
		}
		else
		{
			emit netPriceSelected();
			m_BrokerFilAbnValCheck->hide();
		}
	}
	else
	{
		if (m_CFETSindex == 0)
		{
			if (m_data->bond().is_interest_bond())
			{
				m_CFETSFilAbnValCheck->show();
				m_CFETSFilAbnValCheck->isSelected() == true ? emit filterAbValueSelected(1) : emit filterAbValueSelected(0);
				
			}
			emit yieldSelected();
		}
		else
		{
			emit netPriceSelected();
			m_CFETSFilAbnValCheck->hide();
		}
	}
}

void BondDetailQuoteChartTitle::onBrokerItemSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_data && m_BrokerFilAbnValCheck);
	m_BrokerIndex = index;
	
	if (index == 0)
	{
		emit yieldSelected(); 
		if(m_data->bond().is_interest_bond())	m_BrokerFilAbnValCheck->show();
	}
	else
	{
		emit netPriceSelected();
		m_BrokerFilAbnValCheck->hide();
	}
}

void BondDetailQuoteChartTitle::onCFETSItemSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_data && m_CFETSFilAbnValCheck);
	m_CFETSindex = index;

	if (index == 0)
	{
		emit yieldSelected();
		if (m_data->bond().is_interest_bond())	m_CFETSFilAbnValCheck->show();
	}
	else
	{
		emit netPriceSelected();
		m_CFETSFilAbnValCheck->hide();
	}
}

BondDetailRateChartTitle::BondDetailRateChartTitle(QWidget* parent)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setupUi();
}

BondDetailRateChartTitle::~BondDetailRateChartTitle()
{

}

void BondDetailRateChartTitle::setBondInfo(QSharedPointer<DetailData> data)
{
	QB_CHECK_RETURN_VOID1(data);

	if (data)
	{
		m_bondType = data->getBondType();
	}

	m_data = data;

    connect(m_data.get(), &DetailData::bondTypeChanged, this, &BondDetailRateChartTitle::onBondTypeChanged);

	refreshUi();
}

void BondDetailRateChartTitle::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);

	painter.fillRect(rect(), StyleMgr::instance().color("BasicPlate"));

	QRect titleR = rect();
	titleR.setHeight(24);

	QString title;
	QFontMetrics fm(font());

	if (m_data && m_data->isShowYieldChart())
	{
		title = getYieldCurveTitle();
	}
	else
	{
		title = tr("中债收益率曲线");
	}

	int widthTxt = fm.horizontalAdvance(title);
	int titleX = (titleR.center().x() - widthTxt / 2);

	QRect titleRect;
    titleRect.setRect(titleX, titleR.y(), widthTxt, titleR.height());

	painter.setPen(StyleMgr::instance().color("TextNormal"));
	painter.drawText(titleRect, title, QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
}

void BondDetailRateChartTitle::setupUi()
{
	QVBoxLayout* m_rateVLayout = new QVBoxLayout(this);
	m_rateVLayout->setContentsMargins(4, 4, 4, 0);

	m_BrokerCreditSpreadTab = new STabCtrl(this);
	m_BrokerCreditSpreadTab->addTab(tr("信用利差"));
	m_BrokerCreditSpreadTab->addTab(tr("中债收益率曲线"));
	m_BrokerCreditSpreadTab->selectTab(0);
	m_BrokerCreditSpreadTab->setFixedHeight(24);
	m_rateVLayout->addWidget(m_BrokerCreditSpreadTab);
	m_BrokerCreditSpreadTab->hide();
	
	m_CFETSCreditSpreadTab = new STabCtrl(this);
	m_CFETSCreditSpreadTab->addTab(tr("信用利差"));
	m_CFETSCreditSpreadTab->addTab(tr("中债收益率曲线"));
	m_CFETSCreditSpreadTab->selectTab(0);
	m_CFETSCreditSpreadTab->setFixedHeight(24);
	m_rateVLayout->addWidget(m_CFETSCreditSpreadTab);
	m_CFETSCreditSpreadTab->hide();

	m_BrokerBondAdIssureTab = new STabCtrl(this);
	m_BrokerBondAdIssureTab->addTab(tr("个券"));
	m_BrokerBondAdIssureTab->addTab(tr("主体"));
	m_BrokerBondAdIssureTab->selectTab(0);
	m_BrokerBondAdIssureTab->setFixedSize(80, 24);
	m_rateVLayout->addWidget(m_BrokerBondAdIssureTab,0, Qt::AlignHCenter);
	m_BrokerBondAdIssureTab->hide();

	m_CFETSBondAdIssureTab = new STabCtrl(this);
	m_CFETSBondAdIssureTab->addTab(tr("个券"));
	m_CFETSBondAdIssureTab->addTab(tr("主体"));
	m_CFETSBondAdIssureTab->selectTab(0);
	m_CFETSBondAdIssureTab->setFixedSize(80, 24);
	m_rateVLayout->addWidget(m_CFETSBondAdIssureTab,0,Qt::AlignHCenter);
	m_CFETSBondAdIssureTab->hide();

	m_spaceItem = new QSpacerItem(0, 24);
	m_rateVLayout->addSpacerItem(m_spaceItem);

	m_BrokerYieldTab = new STabCtrl(this);
	m_BrokerYieldTab->addTab(tr("完整曲线"));
	m_BrokerYieldTab->addTab(tr("成交分布"));
	m_BrokerYieldTab->selectTab(0);
	m_BrokerYieldTab->setFixedSize(150, 24);
	m_rateVLayout->addWidget(m_BrokerYieldTab, 0, Qt::AlignHCenter);
	m_BrokerYieldTab->hide();

	m_CFETSYieldTab = new STabCtrl(this);
	m_CFETSYieldTab->addTab(tr("完整曲线"));
	m_CFETSYieldTab->addTab(tr("成交分布"));
	m_CFETSYieldTab->selectTab(0);
	m_CFETSYieldTab->setFixedSize(150, 24);
	m_rateVLayout->addWidget(m_CFETSYieldTab, 0, Qt::AlignHCenter);
	m_CFETSYieldTab->hide();
	m_rateVLayout->addStretch();

	connect(m_BrokerYieldTab, &STabCtrl::tabSelected, this, &BondDetailRateChartTitle::onBrokerYieldTabSelected);
	connect(m_BrokerCreditSpreadTab, &STabCtrl::tabSelected, this, &BondDetailRateChartTitle::onBrokerCreditSpreadTabSelected);
	connect(m_BrokerBondAdIssureTab, &STabCtrl::tabSelected, this, &BondDetailRateChartTitle::onBrokerBondAdIssureTabSelected);

	connect(m_CFETSYieldTab, &STabCtrl::tabSelected, this, &BondDetailRateChartTitle::onCFETSYieldTabSelected);
	connect(m_CFETSCreditSpreadTab, &STabCtrl::tabSelected, this, &BondDetailRateChartTitle::onCFETSCreditSpreadTabSelected);
	connect(m_CFETSBondAdIssureTab, &STabCtrl::tabSelected, this, &BondDetailRateChartTitle::onCFETSBondAdIssureTabSelected);
}

void BondDetailRateChartTitle::refreshUi()
{
	QB_CHECK_RETURN_VOID1(m_data && m_BrokerYieldTab && m_CFETSYieldTab && m_BrokerCreditSpreadTab && m_CFETSCreditSpreadTab && m_BrokerBondAdIssureTab && m_CFETSBondAdIssureTab);

	if (m_data->isShowYieldChart()) //利率债和信用债中的铁道债show yield chart 
	{
		if (m_bondType == em_Broker) 
		{
			m_CFETSCreditSpreadTab->hide();
			m_CFETSBondAdIssureTab->hide();
			m_CFETSYieldTab->hide();
			m_BrokerCreditSpreadTab->hide();
			m_BrokerBondAdIssureTab->hide();//债券详情内切换债券时，需要hide信用利差tab或个券/主体tab 
			m_spaceItem->changeSize(0, 24);
			m_BrokerYieldTab->show();
		}
		else
		{
			m_BrokerCreditSpreadTab->hide();
			m_BrokerYieldTab->hide();
			m_BrokerBondAdIssureTab->hide();
			m_CFETSCreditSpreadTab->hide();
			m_CFETSBondAdIssureTab->hide();
			m_spaceItem->changeSize(0, 24);
			m_CFETSYieldTab->show();
		}
	}
	else
	{
		setCreditSpreadTabText();
		if (m_bondType == em_Broker)
		{
			m_CFETSCreditSpreadTab->hide();
			m_CFETSBondAdIssureTab->hide();
			m_CFETSYieldTab->hide();
			m_BrokerCreditSpreadTab->show();
			m_BrokerBondAdIssureTab->hide();
			m_BrokerYieldTab->hide();
			m_spaceItem->changeSize(0, 0);
			if (m_BrokerCreditSpreadTab->getSelTab() == 0) //选择信用利差/主体信用利差的第一个Tab
			{
				if (m_nWndType == wndTp_Both) //只有信用利差需要显示个券/主体
				{
					m_BrokerBondAdIssureTab->show();
				}
			}
			else
			{
				m_BrokerYieldTab->show();
			}
		}
		else
		{
			m_BrokerCreditSpreadTab->hide();
			m_BrokerYieldTab->hide();
			m_BrokerBondAdIssureTab->hide();
			m_CFETSCreditSpreadTab->show();
			m_CFETSBondAdIssureTab->hide();
			m_CFETSYieldTab->hide();
			m_spaceItem->changeSize(0, 0);
			if (m_CFETSCreditSpreadTab->getSelTab() == 0) //同理,cfets处一样的操作
			{
				if (m_nWndType == wndTp_Both)
				{
					m_CFETSBondAdIssureTab->show();
				}
			}
			else
			{
				m_CFETSYieldTab->show();
			}
		}
	}
}

void BondDetailRateChartTitle::setCreditSpreadTabText()
{
	QString CSTabFirstText = getCreditSpreadTabFirstText();
	QString CSTabSecondText = getYieldCurveTitle();
	QString tips = tr("个券信用利差 = 个券收益率 - 国开曲线收益率\r\n主体信用利差 = 同主体的个券信用利差均值\r\n\r\n注：私募债、PPN、特别担保等类型的债券暂不提供信用利差。");

	if (m_bondType == em_Broker)
	{
		m_BrokerCreditSpreadTab->setTabText(0, CSTabFirstText);
		m_BrokerCreditSpreadTab->setTabInfo(0, "widgets_circle_info", tips);
		m_BrokerCreditSpreadTab->setTabText(1, CSTabSecondText);
	}
	else
	{
		m_CFETSCreditSpreadTab->setTabText(0, CSTabFirstText);
		m_CFETSCreditSpreadTab->setTabInfo(0, "widgets_circle_info", tips);
		m_CFETSCreditSpreadTab->setTabText(1, CSTabSecondText);
	}
}

void BondDetailRateChartTitle::notify()
{
	QB_CHECK_RETURN_VOID1(m_data && m_BrokerYieldTab && m_CFETSYieldTab);

	if (m_data->isShowYieldChart())
	{
		m_bondType == em_Broker ? emit yieldSelected(m_BrokerYieldTab->getSelTab()) : emit yieldSelected(m_CFETSYieldTab->getSelTab());
	}
	else
	{
		if (m_bondType == em_Broker)
		{
			if (m_BrokerCreditSpreadTab->getSelTab() == 0)
			{
				if (m_nWndType == wndTp_Both)
				{
					emit creditSpreadSelected(m_BrokerBondAdIssureTab->getSelTab());
				}
				else if(m_nWndType == wndTp_Bond)
				{
					emit creditSpreadSelected(0);
				}
				else if (m_nWndType == wndTp_Issuer)
				{
					emit creditSpreadSelected(1);
				}
			}
			else
			{
				m_BrokerYieldTab->getSelTab() == 0 ? emit yieldSelected(0) : emit yieldSelected(1);
			}
		}
		else
		{
			if (m_CFETSCreditSpreadTab->getSelTab() == 0)
			{
				if (m_nWndType == wndTp_Both)
				{
					emit creditSpreadSelected(m_CFETSBondAdIssureTab->getSelTab());
				}
				else if (m_nWndType == wndTp_Bond)
				{
					emit creditSpreadSelected(0);
				}
				else if (m_nWndType == wndTp_Issuer)
				{
					emit creditSpreadSelected(1);
				}
			}
			else
			{
				m_CFETSYieldTab->getSelTab() == 0 ? emit yieldSelected(0) : emit yieldSelected(1);
			}
		}
	}
}

QString BondDetailRateChartTitle::getCreditSpreadTabFirstText()
{
	QB_CHECK_RETURN_VALUE(m_data, "");

	m_nWndType = m_data->getWndType();
	QString strTitle = tr("信用利差"); 
	if (m_nWndType != wndTp_Null && m_nWndType != wndTp_Lock && m_nWndType == wndTp_Issuer) {
		strTitle = tr("主体信用利差");
	}
	return strTitle;
}

QString BondDetailRateChartTitle::getYieldCurveTitle()
{
	QB_CHECK_RETURN_VALUE(m_data, "");

	QString title;

	CAnalysisYieldCurve* yieldChart = BondDetailCache::instance().getChart(m_data->bond().GetCombBondKey()).m_pYieldCurve;

	if (!yieldChart)
	{
		title = tr("中债收益率曲线");
		return title;
	}

	BondDict dictType;
	CBondDict::instance().GetBondDict("CurveType", yieldChart->m_Curve.m_CurveType, dictType);

	title = QString::fromLocal8Bit(dictType.selective_content);

	return title;
}

void BondDetailRateChartTitle::onBrokerCreditSpreadTabSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_BrokerBondAdIssureTab && m_BrokerYieldTab);

	if (index == 0)
	{
		if (m_nWndType == wndTp_Both)
		{
			m_BrokerBondAdIssureTab->show();
			emit creditSpreadSelected(m_BrokerBondAdIssureTab->getSelTab());
		}
		if (m_nWndType == wndTp_Issuer)
		{
			emit creditSpreadSelected(1);
		}
		if (m_nWndType == wndTp_Bond)
		{
			emit creditSpreadSelected(0);
		}
		m_BrokerYieldTab->hide();
	}
	else
	{
		m_BrokerYieldTab->show();
		emit yieldSelected(m_BrokerYieldTab->getSelTab());
		m_BrokerBondAdIssureTab->hide();
	}
}

void BondDetailRateChartTitle::onBrokerBondAdIssureTabSelected(int index)
{
	index == 0 ? emit creditSpreadSelected(0) : emit creditSpreadSelected(1);
}

void BondDetailRateChartTitle::onBrokerYieldTabSelected(int index)
{
	index == 0 ? emit yieldSelected(0) : emit yieldSelected(1);
}

void BondDetailRateChartTitle::onCFETSCreditSpreadTabSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_CFETSBondAdIssureTab && m_CFETSYieldTab);

	if (index == 0)
	{
		if (m_nWndType == wndTp_Both)
		{
			m_CFETSBondAdIssureTab->show();
			emit creditSpreadSelected(m_CFETSBondAdIssureTab->getSelTab());
		}
		if (m_nWndType == wndTp_Issuer)
		{
			emit creditSpreadSelected(1);
		}
		if (m_nWndType == wndTp_Bond)
		{
			emit creditSpreadSelected(0);
		}
		m_CFETSYieldTab->hide();
	}
	else
	{
		m_CFETSYieldTab->show();
		emit creditSpreadSelected(m_CFETSYieldTab->getSelTab());
		m_CFETSBondAdIssureTab->hide();
	}
}

void BondDetailRateChartTitle::onCFETSBondAdIssureTabSelected(int index)
{
	index == 0 ? emit creditSpreadSelected(0) : emit creditSpreadSelected(1);
}

void BondDetailRateChartTitle::onCFETSYieldTabSelected(int index)
{
	index == 0 ? emit yieldSelected(0) : emit yieldSelected(1);
}

void BondDetailRateChartTitle::onBondTypeChanged()
{
	QB_CHECK_RETURN_VOID1(m_data);
	m_bondType = m_data->getBondType();
	refreshUi();
	notify();
}

BondDetailCDCChartTitle::BondDetailCDCChartTitle(QWidget* parent)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	setupUi();
}

BondDetailCDCChartTitle::~BondDetailCDCChartTitle()
{

}

void BondDetailCDCChartTitle::setBondInfo(QSharedPointer<DetailData> data)
{
	QB_CHECK_RETURN_VOID1(data);

	if (data)
	{
		m_bondType = data->getBondType();
	}

	m_data = data;

    connect(m_data.get(), &DetailData::bondTypeChanged, this, &BondDetailCDCChartTitle::onBondTypeChanged);

	refreshUi();
}

void BondDetailCDCChartTitle::changeType(int start, int end)
{
	QString startDay = QString::number(start);
	QString endDay = QString::number(end);

	startD = QDate::fromString(startDay, "yyyyMMdd");
	endD = QDate::fromString(endDay, "yyyyMMdd");
}

void BondDetailCDCChartTitle::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);

	painter.fillRect(rect(), StyleMgr::instance().color("BasicPlate"));
}

void BondDetailCDCChartTitle::setupUi()
{
	QVBoxLayout* m_cdcVLayout = new QVBoxLayout();
	m_cdcVLayout->setContentsMargins(4, 0, 4, 0);
	setLayout(m_cdcVLayout);

	m_BrokerCDCTab = new STabCtrl(this);
	m_BrokerCDCTab->addTab(tr("收益率"));
	m_BrokerCDCTab->addTab(tr("净价"));
	m_BrokerCDCTab->selectTab(0);
	m_BrokerCDCTab->setFixedHeight(24);
	m_cdcVLayout->addWidget(m_BrokerCDCTab,Qt::AlignVCenter);

	m_CFETSCDCTab = new STabCtrl(this);
	m_CFETSCDCTab->addTab(tr("收益率"));
	m_CFETSCDCTab->addTab(tr("净价"));
	m_CFETSCDCTab->selectTab(0);
	m_CFETSCDCTab->setFixedHeight(24);
	m_cdcVLayout->addWidget(m_CFETSCDCTab, Qt::AlignVCenter);
	m_CFETSCDCTab->hide();

	m_cdcVLayout->setSpacing(2);

	m_cdcHLayout = new QHBoxLayout();
	m_cdcHLayout->setContentsMargins(0, 0, 0, 0);
	m_cdcHLayout->setSpacing(4);
	m_cdcVLayout->addLayout(m_cdcHLayout);
	//开头的弹簧
	m_BrokerSpaceItemEst = new QSpacerItem(106,0);
	m_cdcHLayout->addSpacerItem(m_BrokerSpaceItemEst);

	m_CFETSSpaceItemEst = new QSpacerItem(0, 0);
	m_cdcHLayout->addSpacerItem(m_CFETSSpaceItemEst);

	m_BrokerCBox = new SCheckComboBox(this, false, "");
	m_BrokerCBox->setFixedSize(80,24);
	m_BrokerCBox->addItem("估值");
	m_BrokerCBox->addItem("成交");
	m_BrokerCBox->setCheck(0, true); 
	m_BrokerCBox->setCheck(1, true);
	m_checkValue.push_back(1);
	m_checkValue.push_back(1);
	m_cdcHLayout->addWidget(m_BrokerCBox);

	m_CFETSCBox = new SCheckComboBox(this, false, "");
	m_CFETSCBox->setFixedSize(80, 24);
	m_CFETSCBox->addItem("估值");
	m_CFETSCBox->addItem("成交");
	m_CFETSCBox->setCheck(0, true);
	m_CFETSCBox->setCheck(1, true);
	m_checkValue.push_back(1); 
	m_checkValue.push_back(1); //1,1,1,1:broker估值成交/cfets估值成交
	m_cdcHLayout->addWidget(m_CFETSCBox);
	m_CFETSCBox->hide();

	m_BrokerDateTab = new STabCtrl(this);
	m_BrokerDateTab->addTab(tr("1M"));
	m_BrokerDateTab->addTab(tr("3M"));
	m_BrokerDateTab->addTab(tr("6M"));
	m_BrokerDateTab->addTab(tr("YTD"));
	m_BrokerDateTab->addTab(tr("1Y"));
	m_BrokerDateTab->addTab(tr("ALL"));
	m_BrokerDateTab->selectTab(1);
	m_BrokerDateTab->setFixedSize(240, 24);
	m_cdcHLayout->addWidget(m_BrokerDateTab);

	m_CFETSDateTab = new STabCtrl(this);
	m_CFETSDateTab->addTab(tr("1M"));
	m_CFETSDateTab->addTab(tr("3M"));
	m_CFETSDateTab->addTab(tr("6M"));
	m_CFETSDateTab->addTab(tr("YTD"));
	m_CFETSDateTab->addTab(tr("1Y"));
	m_CFETSDateTab->addTab(tr("ALL"));
	m_CFETSDateTab->selectTab(1);
	m_CFETSDateTab->setFixedSize(240, 24);
	m_cdcHLayout->addWidget(m_CFETSDateTab);
	m_CFETSDateTab->hide();

	m_BrokerDateBtn = new SImageButton(this, "widgets_calendar");
	m_cdcHLayout->addWidget(m_BrokerDateBtn);

	m_BrokerDualCalendars = new DualCalendars(this, false);
	m_BrokerDualCalendars->hide();

	m_CFETSDateBtn = new SImageButton(this, "widgets_calendar");
	m_cdcHLayout->addWidget(m_CFETSDateBtn);
	m_CFETSDateBtn->hide();

	m_CFETSDualCalendars = new DualCalendars(this, false);
	m_CFETSDualCalendars->hide();

	m_cdcHLayout->addStretch(4);
	//Broker
	connect(m_BrokerCDCTab, &STabCtrl::tabSelected, this, &BondDetailCDCChartTitle::onBrokerTabSelected);
	connect(m_BrokerCBox, &SCheckComboBox::selectionChanged, this, &BondDetailCDCChartTitle::onBrokerChecked);
	connect(m_BrokerDateTab, &STabCtrl::tabSelected, this, &BondDetailCDCChartTitle::onBrokerDateSelected);
	
	//CFETS
	connect(m_CFETSCDCTab, &STabCtrl::tabSelected, this, &BondDetailCDCChartTitle::onCFETSTabSelected);
	connect(m_CFETSCBox, &SCheckComboBox::selectionChanged, this, &BondDetailCDCChartTitle::onCFETSChecked);
	connect(m_CFETSDateTab, &STabCtrl::tabSelected, this, &BondDetailCDCChartTitle::onCFETSDateSelected);

	connect(m_BrokerDateBtn, &SImageButton::clicked, this, &BondDetailCDCChartTitle::onBrokerDateBtnSelected);
	connect(m_CFETSDateBtn, &SImageButton::clicked, this, &BondDetailCDCChartTitle::onCFETSDateBtnSelected);
}

void BondDetailCDCChartTitle::onBondTypeChanged()
{
	QB_CHECK_RETURN_VOID1(m_data);
	m_bondType = m_data->getBondType();
	refreshUi();
	notify();
}

void BondDetailCDCChartTitle::refreshUi()
{
	QB_CHECK_RETURN_VOID1(m_CFETSCDCTab && m_CFETSDateTab && m_BrokerCDCTab && m_BrokerDateTab
					   && m_CFETSSpaceItemEst && m_BrokerSpaceItemEst  && m_BrokerDateBtn && m_CFETSDateBtn);

	bool boptionBond = optionBond();
	
	if (m_bondType == em_Broker)
	{
		m_CFETSCDCTab->hide();
		m_CFETSSpaceItemEst->changeSize(0, 0);
		m_CFETSCBox->hide();
		m_CFETSDateTab->hide();
		m_CFETSDateBtn->hide();

		m_BrokerCDCTab->show();
		m_BrokerSpaceItemEst->changeSize(106, 0);

		if (m_BrokerCDCTab->getSelTab() == 0)
		{
			m_BrokerCBox->show();
		}
		else
		{
			if (boptionBond == true)	
			{
				m_BrokerCBox->hide();
				m_BrokerSpaceItemEst->changeSize(190, 0);
			}
			else
			{
				m_BrokerSpaceItemEst->changeSize(106, 0);
				m_BrokerCBox->show();
			}
		}
		m_BrokerDateTab->show();
		m_BrokerDateBtn->show();
	}
	else
	{
		m_BrokerCDCTab->hide();
		m_BrokerSpaceItemEst->changeSize(0, 0);
		m_BrokerCBox->hide();
		m_BrokerDateTab->hide();
		m_BrokerDateBtn->hide();

		m_CFETSCDCTab->show();
		m_CFETSSpaceItemEst->changeSize(106, 0);

		if (boptionBond == true)
		{
			if (m_CFETSCDCTab->getSelTab() == 0)
			{
				m_CFETSCBox->show();
			}
			else
			{
				m_CFETSCBox->hide();
				m_CFETSSpaceItemEst->changeSize(190, 0);
			}
		}
		else
		{
			m_CFETSCBox->show();
		}

		m_CFETSDateTab->show();
		m_CFETSDateBtn->show();
	}
}

void BondDetailCDCChartTitle::notify()
{
	QB_CHECK_RETURN_VOID1(m_BrokerCBox && m_BrokerCDCTab && m_CFETSCDCTab && m_BrokerDateTab && m_CFETSDateTab);

	bool boptionBond = optionBond();

	bool brokerEstimate = m_BrokerCBox->getCheck(0);
	bool brokerDeal = m_BrokerCBox->getCheck(1);

	bool CFETSEstimate = m_CFETSCBox->getCheck(0);
	bool CFETSDeal = m_CFETSCBox->getCheck(1);

	if (m_bondType == em_Broker)
	{
		if (m_BrokerCDCTab->getSelTab() == 0)
		{
			emit yieldTabSelected(brokerEstimate, brokerDeal);
		}
		else
		{
			boptionBond == true ? emit netPriceTabSelected(0, brokerDeal): emit netPriceTabSelected(brokerEstimate, brokerDeal);
		}
		emit dateTabSelected(m_BrokerDateTab->getSelTab());
	}
	else
	{
		if (m_CFETSCDCTab->getSelTab() == 0)
		{
			emit yieldTabSelected(CFETSEstimate, CFETSDeal);
		}
		else
		{
			boptionBond == true ? emit netPriceTabSelected(0, CFETSDeal) : emit netPriceTabSelected(CFETSEstimate, CFETSDeal);
		}
		emit dateTabSelected(m_CFETSDateTab->getSelTab());
	}
}

bool BondDetailCDCChartTitle::optionBond()
{
	QB_CHECK_RETURN_BOOL1(m_data);
	bool bRight = false;
	if (strcmp(m_data->bond().GetOptionType(), "CAL") == 0 ||
		strcmp(m_data->bond().GetOptionType(), "PUT") == 0 ||
		strcmp(m_data->bond().GetOptionType(), "CNP") == 0) {
		bRight = true;
		if (!m_data->bond().IsRight()) {
			bRight = false;
		}
	}
	return bRight;
}

void BondDetailCDCChartTitle::clearCalendarlastSelected()
{
	QDate date;
	if (m_bondType == em_Broker)
	{
		m_BrokerDualCalendars->getLeftSCalendar()->setStart(date);
		m_BrokerDualCalendars->getRightSCalendar()->setStart(date);
		m_BrokerDualCalendars->getLeftSCalendar()->setEnd(date);
		m_BrokerDualCalendars->getRightSCalendar()->setEnd(date);
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
		m_BrokerDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
	}
	else
	{
		m_CFETSDualCalendars->getLeftSCalendar()->setStart(date);
		m_CFETSDualCalendars->getRightSCalendar()->setStart(date);
		m_CFETSDualCalendars->getLeftSCalendar()->setEnd(date);
		m_CFETSDualCalendars->getRightSCalendar()->setEnd(date);
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
		m_CFETSDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(QDate(), QDate());
	}
}

void BondDetailCDCChartTitle::onBrokerTabSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_BrokerDateTab && m_CFETSSpaceItemEst && m_BrokerSpaceItemEst && m_BrokerCBox && m_CFETSCBox);

	bool boptionBond = optionBond();

	bool brokerEstimateChecked = m_BrokerCBox->getCheck(0);
	bool brokerDealChecked = m_BrokerCBox->getCheck(1);

	m_CFETSSpaceItemEst->changeSize(0, 0);
	m_BrokerSpaceItemEst->changeSize(106, 0);

	if (index == 0)
	{
		m_BrokerCBox->show();
		emit yieldTabSelected(brokerEstimateChecked,brokerDealChecked);
	}
	else
	{
		if (boptionBond)
		{	//根据修改后的逻辑：含权债，选择净价，默认无估值，有成交(无需根据Tab判断)
			m_BrokerSpaceItemEst->changeSize(190, 0);
			m_BrokerCBox->hide();
			emit netPriceTabSelected(0, 1);
		}
		else
		{
			emit netPriceTabSelected(brokerEstimateChecked,brokerDealChecked);
		}
	}
	emit dateTabSelected(m_BrokerDateTab->getSelTab());
}

void BondDetailCDCChartTitle::onCFETSTabSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_CFETSDateTab && m_CFETSSpaceItemEst && m_CFETSCBox);

	bool boptionBond = optionBond();

	int CFETSEstimateChecked = m_CFETSCBox->getCheck(0);
	int CFETSDealChecked = m_CFETSCBox->getCheck(1);

	m_BrokerSpaceItemEst->changeSize(0, 0);
	m_CFETSSpaceItemEst->changeSize(106, 0);

	if (index == 0)
	{
		m_CFETSCBox->show();
		emit yieldTabSelected(CFETSEstimateChecked, CFETSDealChecked);
	}
	else
	{
		if (boptionBond)
		{
			m_CFETSSpaceItemEst->changeSize(190, 0);
			m_CFETSCBox->hide();
			emit netPriceTabSelected(0,1);
		}
		else
		{
			m_CFETSCBox->show();
			emit netPriceTabSelected(CFETSEstimateChecked, CFETSDealChecked);
		}
	}
	emit dateTabSelected(m_CFETSDateTab->getSelTab());
}

void BondDetailCDCChartTitle::onBrokerChecked()
{
	QB_CHECK_RETURN_VOID1(m_BrokerCBox && m_BrokerCDCTab && m_BrokerDateTab);

	bool boptionBond = optionBond();

	bool brokerEstimateChecked = m_BrokerCBox->getCheck(0);
	bool brokerDealChecked = m_BrokerCBox->getCheck(1);

	//必须选择其中一个
	if (!brokerEstimateChecked && !brokerDealChecked)
	{//置回原来的操作
		if (m_checkValue[0] != (int)brokerEstimateChecked)
		{
			m_BrokerCBox->setSelectStated(0, m_checkValue[0]);
		}
		else
		{
			m_BrokerCBox->setSelectStated(1, m_checkValue[1]);
		}
	}
	else
	{
		//修改vector中记录 check点击值
		m_checkValue[0] = brokerEstimateChecked;
		m_checkValue[1] = brokerDealChecked;

		if (m_BrokerCDCTab->getSelTab() == 0)
		{
			emit yieldTabSelected(brokerEstimateChecked, brokerDealChecked);
		}
		else
		{
			boptionBond == true ? emit netPriceTabSelected(0, 1) : emit netPriceTabSelected(brokerEstimateChecked, brokerDealChecked);
		}
		emit dateTabSelected(m_BrokerDateTab->getSelTab());
	}
}

void BondDetailCDCChartTitle::onCFETSChecked()
{
	QB_CHECK_RETURN_VOID1(m_CFETSCBox && m_CFETSCDCTab && m_CFETSDateTab);

	bool boptionBond = optionBond();

	bool CFETSEstimateChecked = m_CFETSCBox->getCheck(0);
	bool CFETSDealChecked = m_CFETSCBox->getCheck(1);

	//必须选择其中一个
	if (!CFETSEstimateChecked && !CFETSDealChecked)
	{//置回原来的操作,不发生任何改变
		if (m_checkValue[2] != (int)CFETSEstimateChecked)
		{
			m_CFETSCBox->setSelectStated(0, m_checkValue[2]);
		}
		else
		{
			m_CFETSCBox->setSelectStated(1, m_checkValue[3]);
		}
	}
	else
	{
		//修改vector中记录 check点击值
		m_checkValue[2] = CFETSEstimateChecked;
		m_checkValue[3] = CFETSDealChecked;

		if (m_CFETSCDCTab->getSelTab() == 0)
		{
			emit yieldTabSelected(CFETSEstimateChecked, CFETSDealChecked);
		}
		else
		{
			boptionBond == true ? emit netPriceTabSelected(0, 1) : emit netPriceTabSelected(CFETSEstimateChecked, CFETSDealChecked);
		}
		emit dateTabSelected(m_CFETSDateTab->getSelTab());
	}
}

void BondDetailCDCChartTitle::onBrokerDateSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_BrokerCBox && m_BrokerCDCTab);

	bool brokerEstimateChecked = m_BrokerCBox->getCheck(0);
	bool brokerDealChecked = m_BrokerCBox->getCheck(1);

	bool boptionBond = optionBond();

	if (m_BrokerCDCTab->getSelTab() == 0)
	{
		emit yieldTabSelected(brokerEstimateChecked, brokerDealChecked);
	}
	else
	{
		boptionBond == true ? emit netPriceTabSelected(0, brokerDealChecked) : emit netPriceTabSelected(1, brokerDealChecked);
	}
	emit dateTabSelected(index);
}

void BondDetailCDCChartTitle::onCFETSDateSelected(int index)
{
	QB_CHECK_RETURN_VOID1(m_CFETSCBox && m_CFETSCDCTab);

	bool CFETSEstimateChecked = m_CFETSCBox->getCheck(0);
	bool CFETSDealChecked = m_CFETSCBox->getCheck(1);

	bool boptionBond = optionBond();

	if (m_CFETSCDCTab->getSelTab() == 0)
	{
		emit yieldTabSelected(CFETSEstimateChecked, CFETSDealChecked);
	}
	else
	{
		boptionBond == true ? emit netPriceTabSelected(0, 1) : emit netPriceTabSelected(CFETSEstimateChecked, CFETSDealChecked);
	}
	emit dateTabSelected(index);
}

void BondDetailCDCChartTitle::onBrokerDateBtnSelected()
{
	QB_CHECK_RETURN_VOID1(m_BrokerDualCalendars && m_BrokerDateBtn);
	int w = m_BrokerDualCalendars->width();
	int h = m_BrokerDualCalendars->height();
	QPoint p = m_BrokerDateBtn->pos();
	int x = mapToGlobal(p).x();
	int y = mapToGlobal(p).y();
	m_BrokerDualCalendars->setGeometry(x - w, y - 5, w, h);
	m_BrokerDualCalendars->show();
	clearCalendarlastSelected();

	if(!dateIsValid) //无效日期
	{
		QDate currentDay(QDate::currentDate());
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedDate(currentDay.addMonths(-1));
		m_BrokerDualCalendars->getRightSCalendar()->showToday();
	}
	else
	{
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedDate(startD);
		if (startD.month() == endD.month())
		{
			m_BrokerDualCalendars->getRightSCalendar()->setSelectedDate(endD.addMonths(1));
		}
		else
		{
			m_BrokerDualCalendars->getRightSCalendar()->setSelectedDate(endD);
		}
		m_BrokerDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
		m_BrokerDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
	}
}

void BondDetailCDCChartTitle::onCFETSDateBtnSelected()
{
	QB_CHECK_RETURN_VOID1(m_CFETSDualCalendars && m_CFETSDateBtn);
	int w = m_CFETSDualCalendars->width();
	int h = m_CFETSDualCalendars->height();
	QPoint p = m_CFETSDateBtn->pos();
	int x = mapToGlobal(p).x();
	int y = mapToGlobal(p).y();
	m_CFETSDualCalendars->setGeometry(x - w, y - 5, w, h);
	m_CFETSDualCalendars->show();
	clearCalendarlastSelected();
	if (!dateIsValid) //无效日期
	{
		QDate currentDay(QDate::currentDate());
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedDate(currentDay.addMonths(-1));
		m_CFETSDualCalendars->getRightSCalendar()->showToday();
	}
	else
	{
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedDate(startD);
		if (startD.month() == endD.month())
		{
			m_CFETSDualCalendars->getRightSCalendar()->setSelectedDate(endD.addMonths(1));
		}
		else
		{
			m_CFETSDualCalendars->getRightSCalendar()->setSelectedDate(endD);
		}
		m_CFETSDualCalendars->getLeftSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
		m_CFETSDualCalendars->getRightSCalendar()->setSelectedColorBetweenTwoDatas(startD, endD);
	}
}

void BondDetailCDCChartTitle::onDualCalendarsGetDate(int start, int end)
{
	if (m_bondType == em_Broker)
	{
		if (start == 0 && end == 0)
		{
			dateIsValid = false; 
		}
		else
		{
			changeType(start, end);
			dateIsValid = true; //有效日期
		}
	}
	else
	{
		if (start == 0 && end == 0)
		{
			dateIsValid = false;
		}
		else
		{
			changeType(start, end);
			dateIsValid = true; //有效日期
		}
	}
}

void BondDetailCDCChartTitle::onBondDateBtnChanged()
{
	QB_CHECK_RETURN_VOID1(m_BrokerDateTab && m_CFETSDateTab);
	if (m_bondType == em_Broker)
	{
		m_BrokerDateTab->selectTab(-1);
	}
	else
	{
		m_CFETSDateTab->selectTab(-1);
	}
}
